package com.game.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.game.dao.IRedHeartDao;
import com.game.pojo.RedHeart;
import com.sun.xml.internal.ws.message.RelatesToHeader;
/**
 * 点赞的dao实现类
 * @author Administrator
 *
 */
public class RedHeartDao implements IRedHeartDao {
	
	//注入持久化上下文
	@PersistenceContext(unitName="mms_persistence_guessnumber")
	EntityManager entityManager;
	
	/**
	 * 保存点赞数
	 */
	@Override
	public void saveRedHeartCount(RedHeart redHeart) {
		//先根据用户的openId查到记录，然后将该记录++
		List<RedHeart> resultList = entityManager.createQuery("select r from RedHeart r join r.user u where u.openId = :openId")
						.setParameter("openId", redHeart.getUser().getOpenId())
						.getResultList();
		for (RedHeart redHeart2 : resultList) {
			redHeart2.setCount((redHeart2.getCount())+1);
		}

	}
	
	/**
	 * 获取点赞记录
	 */
	@Override
	public RedHeart getCount(String openId) {
		//先根据用户的openId查到记录，然后将该记录++
		List<RedHeart> resultList = entityManager.createQuery("select r from RedHeart r join r.user u where u.openId = :openId")
										.setParameter("openId", openId)
											.getResultList();
		return resultList.get(0);
	}
	
	
	
}
